package com.ruoyi.module.studyCenter.controller;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.module.studyCenter.vo.StudyCenterVO;
import com.ruoyi.module.studyCenterDetail.domain.StudyCenterDetail;
import com.ruoyi.module.studyCenterDetail.service.IStudyCenterDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.module.studyCenter.domain.StudyCenter;
import com.ruoyi.module.studyCenter.service.IStudyCenterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学习中心Controller
 * 
 * @author yqq
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/api/studyCenter")
public class StudyCenterController extends BaseController
{
    @Autowired
    private IStudyCenterService studyCenterService;

    @Autowired
    private IStudyCenterDetailService centerDetailService;

    /**
     * 查询学习中心列表
     */
    @PreAuthorize("@ss.hasPermi('api:studyCenter:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudyCenter studyCenter)
    {
        startPage();
        List<StudyCenter> list = studyCenterService.selectStudyCenterList(studyCenter);
        return getDataTable(list);
    }

    /**
     * 导出学习中心列表
     */
    @PreAuthorize("@ss.hasPermi('api:studyCenter:export')")
    @Log(title = "学习中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudyCenter studyCenter)
    {
        List<StudyCenter> list = studyCenterService.selectStudyCenterList(studyCenter);
        ExcelUtil<StudyCenter> util = new ExcelUtil<StudyCenter>(StudyCenter.class);
        util.exportExcel(response, list, "学习中心数据");
    }

    /**
     * 获取学习中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:studyCenter:query')")
    @GetMapping(value = "/{studyId}")
    public AjaxResult getInfo(@PathVariable("studyId") Long studyId)
    {
        StudyCenterVO studyCenterVO = new StudyCenterVO();
        StudyCenter studyCenter = studyCenterService.selectStudyCenterByStudyId(studyId);
        if (Objects.isNull(studyCenter)){
            return AjaxResult.success("不存在");
        }
        BeanUtils.copyProperties(studyCenter,studyCenterVO);
        StudyCenterDetail studyCenterDetail = new StudyCenterDetail();
        studyCenterDetail.setsDetailId(studyId);
        List<StudyCenterDetail> studyCenterDetails = centerDetailService.selectStudyCenterDetailList(studyCenterDetail);
        if (CollUtil.isNotEmpty(studyCenterDetails)){
            //获取简介
            List<StudyCenterDetail> introList = studyCenterDetails.stream().filter(a->"1".equals(a.getDetailBigTitle())).collect(Collectors.toList());
            //获取功能作用
            List<StudyCenterDetail> functionalList = studyCenterDetails.stream().filter(a -> "2".equals(a.getDetailBigTitle())).collect(Collectors.toList());
            studyCenterVO.setIntroList(introList);
            studyCenterVO.setFunctionalList(functionalList);
        }
        return success(studyCenterVO);
    }

    /**
     * 新增学习中心
     */
    @PreAuthorize("@ss.hasPermi('api:studyCenter:add')")
    @Log(title = "学习中心", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody StudyCenterVO vo)
    {
        studyCenterService.insertAllStudyCenter(vo);
        return AjaxResult.success();
    }

    /**
     * 修改学习中心
     */
    @PreAuthorize("@ss.hasPermi('api:studyCenter:edit')")
    @Log(title = "学习中心", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody StudyCenterVO studyCenter)
    {
        studyCenterService.updateAllStudyCenter(studyCenter);
        return AjaxResult.success();
    }

    /**
     * 删除学习中心
     */
    @PreAuthorize("@ss.hasPermi('api:studyCenter:remove')")
    @Log(title = "学习中心", businessType = BusinessType.DELETE)
	@GetMapping("/delete/{studyId}")
    public AjaxResult remove(@PathVariable Long studyId)
    {
        studyCenterService.deleteAllStudyCenter(studyId);
        return AjaxResult.success();
    }
}
